<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">通用大数据框架智能计算引擎优化仿真平台</el-breadcrumb-item>
      <el-breadcrumb-item>docker</el-breadcrumb-item>
      <el-breadcrumb-item>镜像管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="15">
        <el-col :span="8">
          <!--          <el-input-->
          <!--              v-model="search" style="width: 200px" prefix-icon="el-icon-search" placeholder="请输入容器名称" />-->

          <el-input placeholder="请输入内容"  v-model="search" class="input-with-select"  clearable >
            <el-button slot="append" icon="el-icon-search" @click="getoptimizeSchemeList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addOptimizeSchemeVisble = true" >添加镜像</el-button>
        </el-col>
      </el-row>
      <el-table
          :data="optimizeSchemeList.filter(data => !search || data.imageid.toLowerCase().includes(search.toLowerCase())).slice((queryInfo.pagenum-1)*queryInfo.pagesize,queryInfo.pagenum*queryInfo.pagesize)"
          border
          style="width: 100%">
        <el-table-column
            prop="imageid"
            label="镜像ID"
            width="200">
        </el-table-column>
        <el-table-column
            prop="imagetime"
            label="创建时间"
            width="180">
        </el-table-column>
        <el-table-column
            prop="imageoperation"
            label="操作系统"
            width="180">
        </el-table-column>
        <el-table-column
            prop="imagestatus"
            label="状态"
            width="150">
        </el-table-column>
        <el-table-column
            prop="imagecompose"
            label="镜像组成"
            width="200">
        </el-table-column>
        <el-table-column
            prop="imageexample"
            label="关联实例"
            width="200">
        </el-table-column>
        <el-table-column
            prop="operate"
            label="操作"
            width="200">
          <template v-slot="scope">
            <el-tooltip class="item" effect="dark" content="编辑镜像" placement="top">
              <el-button type="primary" icon="el-icon-edit" @click="editOptimizeScheme(scope.row)" ></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除镜像" placement="top">
              <el-button type="danger" icon="el-icon-delete" @click="removeOptimizeScheme(scope.$index)" ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="SizeChange"
          @current-change="CurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[10,20,30]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="optimizeSchemeList.length">
      </el-pagination>

    </el-card>

    <el-dialog
        title="编辑镜像"
        :visible.sync="editOptimizeSchemeVisble"
        width="30%"
        @close="closeeditOptimizeSchemeDialog"
    >

      <el-form>

        <el-input placeholder="修改镜像ID" v-model="imageID" ></el-input>

        <el-form-item label="修改日期:">
          <el-date-picker
              v-model="value1"
              type="date"
              placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作系统:">
          <el-select v-model="opertaion1" placeholder="请选择">
            <el-option
                v-for="item in operation"
                :key="item.opertaion1"
                :label="item.label5"
                :value="item.opertaion1">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态 :">
          <el-select v-model="status1" placeholder="请选择">
            <el-option
                v-for="item in status"
                :key="item.status1"
                :label="item.label4"
                :value="item.status1">
            </el-option>
          </el-select>
        </el-form-item>
        <el-input placeholder="修改镜像组成" v-model="imageCompose" ></el-input>
        <el-input placeholder="修改关联实例" v-model="imageExample" ></el-input>
      </el-form>

      <span slot="footer" class="dialog-footer">
     <el-button @click="editOptimizeSchemeVisble = false">取 消</el-button>
     <el-button type="primary" @click="editOptimizeSchemeList">确 定</el-button>
     </span>
    </el-dialog>

    <el-dialog
        title="添加镜像"
        :visible.sync="addOptimizeSchemeVisble"
        width="30%"
    >

      <el-form>
        <el-form-item label="镜像ID:">
          <el-input placeholder="推荐值：nginx-wsl" v-model="Createimage.imageid" ></el-input>
        </el-form-item>
        <el-form-item label="创建时间:">
          <el-input placeholder="示例：2022/4/1" v-model="Createimage.imagetime"></el-input>
        </el-form-item>

        <el-form-item label="操作系统:">
          <el-input placeholder="示例:linux" v-model="Createimage.imageoperation" ></el-input>
        </el-form-item>
        <el-form-item label="状态:">
          <el-input placeholder="示例：可用" v-model="Createimage.imagestatus" ></el-input>
        </el-form-item>
        <el-form-item label="镜像组成:">
          <el-input placeholder="示例：系统盘（40G）" v-model="Createimage.imagecompose" ></el-input>
        </el-form-item>
        <el-form-item label="关联实例:">
          <el-input placeholder="示例：当前项目尚未创建实例" v-model="Createimage.imageexample" ></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
     <el-button @click="addOptimizeSchemeVisble = false">取 消</el-button>
     <el-button type="primary" @click="add_image">确 定</el-button>
     </span>
    </el-dialog>

  </div>
</template>
<script>
import {add_image,del_image,find_all_image} from "@/api/file";
export default {
  data(){
    return{
      search:'',
      value1:'',
      imageCompose:'',
      imageExample:'',
      opertaion1:'',
      status1:'',
      imageID:'',
      taskname:'',
      value:'',
      namespace:{
        imagestatus:"可用"
      },
      Createimage:{
        imageid:'',
        imagetime:'',
        imageoperation:'',
        imagestatus:'',
        imagecompose:'',
        imageexample:'',
      },
      remove_image:{
        imageid:'',
        imagetime:'',
        imageoperation:'',
        imagestatus:'',
        imagecompose:'',
        imageexample:'',
        // container_namespace:'bigdata-platform',
      },
      test:[
        {
          imageid:'nginx-wsl1',
          imagetime:'2021/11/11',
          imageoperation:'linux',
          imagestatus:'可用',
          imagecompose:'系统盘（40G）',
          imageexample:'当前镜像尚未创建实例',
        },
        {
          imageid:'nginx-wsl1',
          imagetime:'2021/11/11',
          imageoperation:'linux',
          imagestatus:'可用',
          imagecompose:'系统盘（40G）',
          imageexample:'当前镜像尚未创建实例',
        },
        {
          imageid:'admin',
          imagetime:'2021/11/11',
          imageoperation:'linux',
          imagestatus:'可用',
          imagecompose:'系统盘（40G）',
          imageexample:'当前镜像尚未创建实例',
        }

      ],
      queryInfo:{
        //查询参数
        query:'',
        pagenum:1,
        pagesize:10
      },
      total:0,
      optimizeSchemeList:[],
      editOptimizeSchemeParams: {

      },
      editOptimizeSchemeVisble : false,
      addOptimizeSchemeVisble : false,
      addOptimizeSchemefromlocalVisble:false,
    }
  },
  methods:{
    SizeChange(newsize){
      this.queryInfo.pagesize = newsize
      this. getoptimizeSchemeList()
    },
    CurrentChange(newnumber){
      this.queryInfo.pagenum = newnumber
      this. getoptimizeSchemeList()
    },
    closeeditOptimizeSchemeDialog(){
      this.$refs.editOptimizeSchemeFormRef.resetFields()
    },
    getoptimizeSchemeList() {
      // this.optimizeSchemeList=this.test
      find_all_image(this.namespace).then(res=>{

        this.optimizeSchemeList = res
      })
    },
    editOptimizeScheme(row){
      this.editOptimizeSchemeParams = row
      this.editOptimizeSchemeVisble = true
    },
    editOptimizeSchemeList(){
      this.$http.put("/${this.editOptimizeSchemeParams.imageid}",this. editOptimizeSchemeParams).then(res=>{
        if(res.data.meta.status !== 200) return this.$message.error('更新失败')
        this.getoptimizeSchemeList()
      })
    },

    removeOptimizeScheme(index){
      console.log("镜像名字"+this.optimizeSchemeList[index].imageid)
      this.$confirm('此操作将永久删除'+this.optimizeSchemeList[index].imageid+', 是否继续?', '删除方案', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.remove_image.imageid = this.optimizeSchemeList[index].imageid
        del_image(this.remove_image)
        this.optimizeSchemeList.splice(index,1)  // 在前端页面删除
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    add_image(){
      add_image(this.Createimage).then(res=>{
        console.log("res:")
        console.log(res)
        console.log(res.toString() == "Created successfully")
        // let result = res
        // console.log(result.class)
        if(res.toString() == "Created successfully"){
          this.success_tip()
          this.optimizeSchemeList.push(this.Createimage)
        }else{
          this.fail_tip()
        }
        // 逻辑处理
      }).catch(err=>{
        console.log(err)
        this.$message.error(err);
      })
    },
    success_tip() {
      this.$message({
        message: '创建成功',
        type: 'success'
      });
    },
    fail_tip(){
      this.$message({
        message: '创建失败',
        type: 'fail'
      });
    },
  },
  created() {
    console.log("created")
    this.getoptimizeSchemeList()
  }
}
</script>
<style>
</style>